package com.imvc.util;


import sun.plugin.com.TypeConverter;

import java.awt.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工具类
 *
 * @author ml
 * @create 2017-10-18--10:56
 */
public final class CommonUtil {

    private static final List<String> PATTERNS = new ArrayList<>();


    static {
        PATTERNS.add("yyyy-MM-dd");
        PATTERNS.add("yyyy-MM-dd HH:mm:ss");
    }

    //单例模式
    private CommonUtil() {
        throw new AssertionError();
    }

    /**
     * 字符串首字母大写
     *
     * @param str
     * @return
     */
    public static String capitalize(String str) {
        StringBuilder sb = new StringBuilder();
        if (str != null && str.length() > 0) {
            sb.append(str.substring(0, 1).toUpperCase());
            if (str.length() > 1) {
                sb.append(str.substring(1));
            }
            return sb.toString();
        }
        return str;
    }

    /**
     * 生成随机颜色
     *
     * @return
     */
    public static Color getRandomColor() {
        int r = (int) (Math.random() * 256);
        int g = (int) (Math.random() * 256);
        int b = (int) (Math.random() * 256);
        return new Color(r, g, b);
    }

    public static void registerDateTimePattern(String pattern) {
        PATTERNS.add(pattern);
    }

    public static void unRegisterDateTimePattern(String pattern) {

        PATTERNS.remove(pattern);
    }


    /**
     * 将字符串转换成日期类型
     *
     * @param str
     * @return
     */
    public static Date convertStringToDateTime(String str) {
        if (str != null) {
            for (String pattern : PATTERNS) {
                Date date = tryConvertStringToDate(str, pattern);

                if (date != null) {
                    return date;
                }
            }
        }
        return null;
    }


    private static Date tryConvertStringToDate(String str, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        dateFormat.setLenient(false);//不符合样式的不给转换

        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {

            return null;
        }
    }

    /**
     * 将日期转换为字符串
     *
     * @param date    时间日期对象
     * @param pattern 样式字符串
     * @return 转换后的字符串
     */
    public static String convertDateTimeToString(Date date, String pattern) {

        return new SimpleDateFormat(pattern).format(date);
    }

    public static Object changeStringToObject(Class<?> elemType, String value) {
        Object tempObj = null;
        //转换成字节
        if (elemType == byte.class || elemType == Byte.class) {
            tempObj = Byte.parseByte(value);
        } else if (elemType == short.class || elemType == Short.class) {
            tempObj = Short.parseShort(value);
        } else if (elemType == int.class || elemType == Integer.class) {
            tempObj = Integer.parseInt(value);
        } else if (elemType == long.class || elemType == Long.class) {
            tempObj = Long.parseLong(value);
        } else if (elemType == double.class || elemType == Double.class) {
            tempObj = Double.parseDouble(value);
        } else if (elemType == float.class || elemType == Float.class) {
            tempObj = Float.parseFloat(value);
        } else if (elemType == boolean.class || elemType == Boolean.class) {
            tempObj = Boolean.parseBoolean(value);
        } else if (elemType == Date.class) {
            tempObj = convertStringToDateTime(value);
        } else if (elemType == String.class) {
            tempObj = value;
        } else {

        }


        return tempObj;

    }

    /**
     * 获取文件后缀名
     * @param fileName
     * @return
     */
    public static String getFileSuffix(String fileName) {
        int index = fileName.lastIndexOf(".");
        return index > 0 ? fileName.substring(index) : "";
    }
}
